﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;
using BiografController;

namespace BiografGUI
{
    public partial class FilmGUI : Form
    {
        string result;
        FilmController fCtr;
        List<String> _items = new List<string>().ToList();

        string[] actorsList;

        public FilmGUI()
        {
            fCtr = new FilmController();
            InitializeComponent();
            textBox1.Text = "Titel";
            textBox2.Text = "Årstal (Valgfri)";
            plotLbl.Text = "";
        }

        private void textBox1_Enter(object sender, EventArgs e)
        {
            textBox1.Text = "";
        }

        private void textBox2_Enter(object sender, EventArgs e)
        {
            textBox2.Text = "";
        }

        private void textBox1_Leave(object sender, EventArgs e)
        {
            if (textBox1.Text.Equals(""))
            {
                textBox1.Text = "Titel";
            }
        }

        private void textBox2_Leave(object sender, EventArgs e)
        {
            if (textBox2.Text.Equals(""))
            {
                textBox2.Text = "Årstal (Valgfri)";
            }
        }

        private string getMovieValue(string atr)
        {

            int countAtr = 0;
            countAtr = atr.Length;
            string start = result.Substring(result.IndexOf(atr) + countAtr + 3);
            string done = start.Substring(0, start.IndexOf("\""));
            return done;
        }

        public void getActors()
        {
            try
            {
                int countAtr = 0;
                countAtr = "Actors".Length;
                string actors = result.Substring(result.IndexOf("Actors") + countAtr + 3);
                string firstResult = actors.Substring(0, actors.IndexOf("Plot") - 3);
                string secondResult;

                while (firstResult.Contains(","))
                {
                    string actor = firstResult.Substring(0, firstResult.IndexOf(","));
                    actorsList = new string[] {actor};
                    secondResult = firstResult.Substring(firstResult.IndexOf(",") + 2);
                    firstResult = secondResult;
                }
                actorsList = new string[]{firstResult};
            }
            catch (Exception ex)
            {
                MessageBox.Show("FEJL! " + ex);
            }
        }

        private void Search_Click(object sender, EventArgs e)
        {
            try
            {
                progressBar1.Value = 0;

                int year;
                if (textBox2.Text == "Årstal (Valgfri)")
                    year = 0;
                else
                    year = Convert.ToInt32(textBox2.Text);

                progressBar1.Value = 50;
                result = fCtr.GetFilm(textBox1.Text, year);

                titleLbl.Text = getMovieValue("Title");
                yearLbl.Text = getMovieValue("Year");
                releasedLbl.Text = getMovieValue("Released");
                genreLbl.Text = getMovieValue("Genre");
                progressBar1.Value = 75;
                directorLbl.Text = getMovieValue("Director");
                writerLbl.Text = getMovieValue("Writer");
                getActors();
                listBox1.DataSource = actorsList;
                plotLbl.Text = getMovieValue("Plot");
                runtimeLbl.Text = getMovieValue("Runtime");
                ratingLbl.Text = getMovieValue("Rating") + " / 10";
                progressBar1.Value = 100;


                if (getMovieValue("Poster") != "N/A")
                {
                    pictureBox1.ImageLocation = getMovieValue("Poster").Replace("SX320", "SX240");
                }
                else
                {
                    pictureBox1.Image = global::BiografGUI.Properties.Resources.noImageAvailable1;
                }
            }
            catch (Exception ex)
            {
                MessageBox.Show("Din idiot fejl: " + ex);
            }
        }

        private void button1_Click(object sender, EventArgs e)
        {
            try
            {
                bool aktiv = false;
                if (aktivCheckBox.Checked)
                {
                    aktiv = true;
                }
                else
                {
                    aktiv = false;
                }
                bool er3D = false;
                if (er3DCheckBox.Checked)
                {
                    er3D = true;
                }
                else
                {
                    er3D = false;
                }
                double billetPris = Convert.ToDouble(prisTxtBox.Text);
                fCtr.NyFilm(titleLbl.Text, directorLbl.Text, plotLbl.Text, genreLbl.Text, runtimeLbl.Text, aktiv, pictureBox1.ImageLocation, er3D, billetPris, actorsList);

            }
            catch (Exception ex)
            {
                plotLbl.Text = ex.ToString();
            }
            finally
            {

                MessageBox.Show(this, "Filmen blev oprettet ! :D", "Wheee", MessageBoxButtons.OK);
            }
        }

        private void prisTxtBox_Enter(object sender, EventArgs e)
        {
            prisTxtBox.Text = "";
        }

        private void prisTxtBox_Leave(object sender, EventArgs e)
        {
            if (prisTxtBox.Text.Equals(""))
            {
                prisTxtBox.Text = "Indtast pris"; 
            }
        }

    }
}
